package yt.kpool.activity.miner;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import yt.kpool.R;
import yt.kpool.bean.LineChartTestBean;
import yt.kpool.bean.MinerBean;
import yt.kpool.bean.MinerCharBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.DynamicLineChartManager;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.TimeUtil;
import yt.kpool.util.TitleControl;
import yt.kpool.util.ToastUtil;
import yt.kpool.view.dialog.ErrorDialog;
import yt.kpool.view.dialog.LoadingDialog;

/**
 * 矿工图表页
 */
public class MinerChartActivity extends AppCompatActivity {
    @BindView(R.id.dynamic_chart2)
    LineChart mChart2;
    @BindView(R.id.tv_Hashrate)
    TextView tv_Hashrate;
       @BindView(R.id.tv_Hashrate24H)
    TextView tv_Hashrate24H;
       @BindView(R.id.tv_LastShare)
    TextView tv_LastShare;
       @BindView(R.id.tv_Reject)
    TextView tv_Reject;
       @BindView(R.id.tv_unit)
    TextView tv_unit;

    private LoadingDialog loadingDialog;
    private DynamicLineChartManager dynamicLineChartManager2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_miner_chart);
        ButterKnife.bind(this);
        initTitle();
        initView();

        getData();
    }

    private void initView() {
        tv_Hashrate.setText(getHashrate());
        tv_Hashrate24H.setText(getHashrate24H());
        tv_LastShare.setText(TimeUtil.dataString2String(getLastShare()));
        NumberFormat nf = NumberFormat.getPercentInstance();
        nf.setMaximumFractionDigits(1);
//        tv_Reject.setText(nf.format(getReject()));
        tv_Reject.setText(new DecimalFormat("0.00").format(100*getReject())+"%");
    }

    /**
     * 获取网络数据
     */
    private void getData() {

        if (NetWorkUtil.check(this)) {
            loadingDialog.show();
            NetHelperNew.GetOwnMinersChart(getChildAccountID(),getCurrencyID(), getName(), new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    Log.i("-->", "矿工图表数据：" + data);
                    loadingDialog.dismiss();
                    MinerCharBean minerBean = MyGson.getInstance().fromJson(data, MinerCharBean.class);


                    if (minerBean.getType() == 1) {
                        List<MinerCharBean.DataBean> dataBeans = minerBean.getData();
                        List<LineChartTestBean> list=new ArrayList<>();

                        String unit="";
                        float max = 0;
                        for (int i = 0; i <dataBeans.size(); i++) {
                            if (i==0)
                            {
                                unit=dataBeans.get(i).getHashrateUnit();
                            }
                            try {
                                list.add(new LineChartTestBean(TimeUtil.dealDateFormatNomm(dataBeans.get(i).getCreatedTime()), (float) dataBeans.get(i).getHashrate()));
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            if (dataBeans.get(i).getHashrate() > max) {
                                max = (float) dataBeans.get(i).getHashrate();
                            }
                        }




                        List<String> names = new ArrayList<>(); //折线名字集合
                        List<Integer> colour = new ArrayList<>();//折线颜色集合
                        //折线名字 //折线颜色
                        names.add("矿工");
                        colour.add(getResources().getColor(R.color.white));




                        tv_unit.setText(""+unit);
                        dynamicLineChartManager2 = new DynamicLineChartManager(mChart2, names, colour,48,unit);
                        dynamicLineChartManager2.setDescription("");
                        dynamicLineChartManager2.setYAxis(max, 0, 6);
                        for (int i = 0; i < list.size(); i++) {
                            dynamicLineChartManager2.addEntry(list.get(i));
                        }
                    } else {

                        new ErrorDialog(MinerChartActivity.this, minerBean.getContent()).show();
                    }
                }
                @Override
                public void onError(String msg) {
                    super.onError(msg);

                    loadingDialog.dismiss();
                    ToastUtil.showToast(MinerChartActivity.this, msg);
                }
            });

        }
    }

    /**
     * 跳转方法
     * @param mactivity
     * @param ChildAccountID 子帐号ID
     * @param Name 矿机名称
     */
    public static void start(Activity mactivity, String ChildAccountID,String CurrencyID, String Name,String Hashrate,String Hashrate24H,String LastShare,float Reject)
    {
        Intent intent = new Intent(mactivity, MinerChartActivity.class);
        intent.putExtra("ChildAccountID",ChildAccountID);
        intent.putExtra("CurrencyID",CurrencyID);
        intent.putExtra("Name",Name);
        intent.putExtra("Hashrate",Hashrate);
        intent.putExtra("Hashrate24H",Hashrate24H);
        intent.putExtra("LastShare",LastShare);
        intent.putExtra("Reject",Reject);
        mactivity.startActivity(intent);
    }
    private void initTitle() {
        TitleControl titleControl = new TitleControl(this);
        titleControl.setTitle(getName());
        loadingDialog=new LoadingDialog(this,0);
    }
    public String getName() {
        return getIntent().getStringExtra("Name");
    }
    public String getHashrate() {
        return getIntent().getStringExtra("Hashrate");
    }
    public String getHashrate24H() {
        return getIntent().getStringExtra("Hashrate24H");
    }
    public String getLastShare() {
        return getIntent().getStringExtra("LastShare");
    }
    public float getReject() {
        return getIntent().getFloatExtra("Reject",0f);
    }
    public String getChildAccountID() {
        return getIntent().getStringExtra("ChildAccountID");
    }
    public String getCurrencyID() {
        return getIntent().getStringExtra("CurrencyID");
    }
}
